﻿using LeanCloud;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;

public class Find : MonoBehaviour {

    public Text name;
    public Text id;
    public Text Jz;
    public Text BH;

    IEnumerable <AVObject> results;
    public GameObject originObject;
    public Transform parentTransForm;
    public int idShuLaing;

    // Use this for initialization
    void Start () {
        AVClient.Initialize("bKNobM2wmMsD7ReQ1f2Py91W-gzGzoHsz", "zudIgpWooM5QChpsT4E1yAAY", "https://bknobm2w.lc-cn-n1-shared.com");
        Click();

    }

    // Update is called once per frame
    void Update () {


    }

    public void Click()
    {
        //fuzhi[] dolist = GetComponents<fuzhi>();
        fuzhi[] dolist = FindObjectsOfType<fuzhi>();
        foreach (var item in dolist)
        {
            Destroy(item.gameObject);
        }
        Async();
        OnQueryClicked();
        Invoke("AAA", 1);

        //Thread.Sleep(100);
        //InstantiateList();
    }

    async System.Threading.Tasks.Task Async()
    {
        //AVQuery<AVObject> query = new AVQuery<AVObject>("GameEquip").WhereStartsWith("shop_id", Id_Find.Id);
        AVQuery<AVObject> query = new AVQuery<AVObject>("GameEquipBag");
        AVObject equipment = await query.GetAsync(Id_Find.Id);

        string SYM = equipment.Get<string>("shop_id");
        string Name = equipment.Get<string>("shop_name");
        string FWMQZ = equipment.Get<string>("shop_Firstnum");
        string FWM = equipment.Get<string>("shop_num");
        string PL = equipment.Get<string>("shop_batching");
        string JZL = equipment.Get<string>("shop_weight");
        string SCS = equipment.Get<string>("shop_production");
        string DZ = equipment.Get<string>("shop_address");
        string SCXKZBH = equipment.Get<string>("shop_shengchancode");
        string GMTS = equipment.Get<string>("shop_guomin");


        name.text = Name;
        id.text = SYM;
        Jz.text = JZL;
        BH.text = SCXKZBH;


        Debug.Log(Id_Find.Id);
        Debug.Log(SYM);

        //获取自定义属性
        //string bagName = equipBag.Get<string>("name");

        //获取三个特殊属性
        //string objectId = equipBag.ObjectId;
        //DateTime? createdAt = equipBag.CreatedAt;
        //DateTime? updatedAt = equipBag.UpdatedAt;
    }
    
    public void OnQueryClicked()
    {
        Debug.Log("OnQueryClicked");
        AVQuery<AVObject> query = new AVQuery<AVObject>("GameEquipBag");

        query.Limit(100).OrderByDescending("objectId").FindAsync().ContinueWith(t =>
        {
            idShuLaing = t.Result.Count<object>();
            results = t.Result;

            //IEnumerator<AVObject> enumerator = t.Result.GetEnumerator();
            //Debug.Log("Count:" + t.Result.Count<object>());
        });
        
    }
    public void InstantiateList()
    {
        Debug.Log(idShuLaing);
        foreach (AVObject result in results)
        {
            //foreach (var item in result.Keys)
            //{
            //    Debug.Log("测试" + item);
            //} string objectId = equipBag.ObjectId;
            GameObject go = Instantiate(originObject, parentTransForm);
            fuzhi fz = go.GetComponent<fuzhi>();
            fz.setAll(result.Get<string>("shop_id"), result.Get<string>("shop_name"), result.Get<string>("shop_weight"), result.Get<string>("shop_shengchancode"), result.Get<string>("shop_shengchancode"), result.ObjectId);
            Debug.Log("shop_id:" + result.Get<string>("shop_id") + "---shop_name:" + result.Get<string>("shop_name") + result.ObjectId);
        }
    }

    public void AAA() {
        //for (int i = 0; i < idShuLaing; i++)
        //{
            InstantiateList();
        //}
    }
}
